package ru.sushka.lunch.gui.views;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.graphics.Point;

import ru.sushka.lunch.gui.AbstractOrderController;


public class OrderForm {
    
    private AbstractOrderController orderController;
    private Button removeMenuBtn;
    private Button saveMenuBtn;
    private Button cancelBtn;
    private Button addMenuBtn;
    Table orderTable;
    Table menuTable;
    private Label customerCostLabel;
    private Label totalCostLabel;

    
    public Table getOrderTable() {
        return orderTable;
    }

    public void setOrderTable(Table orderTable) {
        this.orderTable = orderTable;
    }

    public Table getMenuTable() {
        return menuTable;
    }

    public void setMenuTable(Table menuTable) {
        this.menuTable = menuTable;
    }

    public Label getCustomerCostLabel() {
        return customerCostLabel;
    }

    public void setCustomerCostLabel(Label customerCostLabel) {
        this.customerCostLabel = customerCostLabel;
    }

    public Label getTotalCostLabel() {
        return totalCostLabel;
    }

    public void setTotalCostLabel(Label totalCostLabel) {
        this.totalCostLabel = totalCostLabel;
    }

    
    public OrderForm(AbstractOrderController orderController){
        this.orderController = orderController;
    }
    /**
     * Open the window.
     * @wbp.parser.entryPoint
     */
    public void open() {
        Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setMinimumSize(new Point(620, 300));
        shell.setSize(620, 500);
        shell.setText("Заказ");
        GridLayout gl_shell = new GridLayout(1, false);
        shell.setLayout(gl_shell);
        
        Composite customerComposite = new Composite(shell, SWT.NONE);
        RowLayout rl_customerComposite = new RowLayout(SWT.HORIZONTAL);
        rl_customerComposite.center = true;
        rl_customerComposite.wrap = false;
        customerComposite.setLayout(rl_customerComposite);
        GridData gd_customerComposite = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
        gd_customerComposite.heightHint = 34;
        customerComposite.setLayoutData(gd_customerComposite);
        
        Label customerNameLabel = new Label(customerComposite, SWT.HORIZONTAL);
        customerNameLabel.setLayoutData(new RowData(256, SWT.DEFAULT));
        customerNameLabel.setAlignment(SWT.RIGHT);
        
        Composite makeOrderComposite = new Composite(shell, SWT.NONE);
        makeOrderComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        makeOrderComposite.setLayout(new GridLayout(3, false));
        
        orderTable = new Table(makeOrderComposite, SWT.BORDER | SWT.FULL_SELECTION);
        orderTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        orderTable.setHeaderVisible(true);
        orderTable.setLinesVisible(false);
        
        Composite orderControlsComposite = new Composite(makeOrderComposite, SWT.NONE);
        orderControlsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
        orderControlsComposite.setLayout(new RowLayout(SWT.VERTICAL));
        
        addMenuBtn = new Button(orderControlsComposite, SWT.CENTER);
        addMenuBtn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(menuTable.getSelection().length>0){
                    orderController.addOrder(menuTable.getSelection()[0]);
                    updateCostLabels();
                }
            }
        });
        addMenuBtn.setLayoutData(new RowData(32, 32));
        addMenuBtn.setText("<");
        
        removeMenuBtn = new Button(orderControlsComposite, SWT.CENTER);
        removeMenuBtn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(orderTable.getSelection().length>0){
                    updateCostLabels();
                }    
            }
        });
        removeMenuBtn.setLayoutData(new RowData(32, 32));
        removeMenuBtn.setText(">");
        
        menuTable = new Table(makeOrderComposite, SWT.BORDER | SWT.FULL_SELECTION);
        menuTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        menuTable.setHeaderVisible(true);
        menuTable.setLinesVisible(false);
        
        
        Composite costLabelComposite = new Composite(shell, SWT.NONE);
        costLabelComposite.setLayout(new GridLayout(1, false));
        costLabelComposite.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
        
        customerCostLabel = new Label(costLabelComposite, SWT.NONE);
        customerCostLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        customerCostLabel.setAlignment(SWT.RIGHT);
        
        
        totalCostLabel = new Label(costLabelComposite, SWT.NONE);
        totalCostLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        totalCostLabel.setEnabled(true);
        totalCostLabel.setAlignment(SWT.RIGHT);
        
        Composite confirmeOrderComposite = new Composite(shell, SWT.NONE);
        RowLayout rl_confirmeOrderComposite = new RowLayout(SWT.HORIZONTAL);
        rl_confirmeOrderComposite.marginTop = 30;
        confirmeOrderComposite.setLayout(rl_confirmeOrderComposite);
        confirmeOrderComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, false, false, 1, 1));
        
        saveMenuBtn = new Button(confirmeOrderComposite, SWT.NONE);
        saveMenuBtn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                orderController.saveOrderList();
                //TODO messagebox
                shell.dispose();
            }
        });
        saveMenuBtn.setToolTipText("");
        saveMenuBtn.setText("Сохранить");
        
        cancelBtn = new Button(confirmeOrderComposite, SWT.NONE);
        cancelBtn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                shell.dispose();
            }
        });
        cancelBtn.setText("Отмена");
        
        String[] menuTitles = { "Категория","Наименование","Цена"};
        String[] orderTitles = { "Наименование","Цена","Количество"};
        
        
        for (int i = 0; i < orderTitles.length; i++) {
            TableColumn column = new TableColumn(orderTable, SWT.NONE);
            column.setText(orderTitles[i]);
        }
        
        for (int i = 0; i < menuTitles.length; i++) {
            TableColumn column = new TableColumn(menuTable, SWT.NONE);
            column.setText(menuTitles[i]);
        }
        
        for (int i=0; i<menuTitles.length; i++) {
            menuTable.getColumn(i).pack ();
          }

        for (int i=0; i<orderTitles.length; i++) {
            orderTable.getColumn(i).pack ();
        }

        orderController.loadMenuList();
        orderController.loadOrderList();
        

        customerCostLabel.setText("Сумма вашего заказа: " + orderController.getCurrentCustomerCost()+ " руб.");
        totalCostLabel.setText("Общая сумма заказа: " + orderController.getTotalCost()+ " руб." );
        customerNameLabel.setText(orderController.getCustomer().getFirstname() + " " + orderController.getCustomer().getLastname());
        
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
    
    public String showInputDialog(String text,String message, String input){
        InputDialog dlg = new InputDialog(new Shell());
        dlg.setText(text);
        dlg.setMessage(message);
        dlg.setInput(input);
        return dlg.open();
    }
   
    private void updateCostLabels() {
        customerCostLabel.setText("Сумма вашего заказа: " + orderController.getCurrentCustomerCost()+ " руб.");
        totalCostLabel.setText("Общая сумма заказа: " + orderController.getTotalCost()+ " руб." );
        customerCostLabel.setBounds(customerCostLabel.getBounds());
        totalCostLabel.setBounds(totalCostLabel.getBounds());
    }
}
